package sch.jl.caigou.utils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import sch.jl.caigou.db.DbConnection;

public class getMax 
{
	
	private static Connection conn = null;
	private static PreparedStatement ps = null;
	private static ResultSet rs = null;
	
	public static String getMaxBH(String type) throws Exception
	{
		String bh = "";

		
		String sql = "select max(bh) from think_lingxing_sbjj where bh like '"+type+"%'";
		
		conn = DbConnection.getConnection();
		ps = conn.prepareStatement(sql);
		rs = ps.executeQuery();
		
		while(rs.next())
		{
			bh = rs.getString(1);
			////System.out.println(bh);
		}
		DbConnection.free(conn, ps, rs);
		if(bh == null)
		{
			return type+"1";
		}
		bh = bh.charAt(0)+String.valueOf(Integer.valueOf(bh.substring(1, bh.length()))+1);
		return bh;
	}
	
	public static String getMaxSq_Id()throws Exception
	{
		String sq_id = "";
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		String sql = "select max(sq_id) from think_shenqing;";
		
		conn = DbConnection.getConnection();
		ps = conn.prepareStatement(sql);
		rs = ps.executeQuery();
		
		while(rs.next())
		{
			sq_id = rs.getString(1);
			////System.out.println(sq_id);
		}
		DbConnection.free(conn, ps, rs);
		sq_id =String.valueOf(Integer.valueOf(sq_id)+1);
		return sq_id;
	}
	
	public static String getMaxID()throws Exception
	{
		String id = "";
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		String sql = "select max(id) from think_liebiao_sbjj";
		
		conn = DbConnection.getConnection();
		ps = conn.prepareStatement(sql);
		rs = ps.executeQuery();
		while(rs.next())
		{
			id = rs.getString(1);
			////System.out.println(sq_id);
		}
		DbConnection.free(conn, ps, rs);
		id =String.valueOf(Integer.valueOf(id)+1);
		return id;
	}
	
	public static List<String> getLieBiaoID(String bh) throws ClassNotFoundException, SQLException
	{
		List<String> id = new ArrayList<String>();
		String sq_id = null ;
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		String sql1 = "select sq_id from think_lingxing_sbjj where bh = '"+bh+"'";
		//System.out.println(sql1);
		conn = DbConnection.getConnection();
		ps = conn.prepareStatement(sql1);
		rs = ps.executeQuery();
		while(rs.next())
		{
			sq_id = rs.getString("sq_id");
			////System.out.println(sq_id);
		}
		
		String sql = "select id from think_liebiao_sbjj where sq_id = '"+sq_id+"'";
		ps = conn.prepareStatement(sql);
		//System.out.println(sql);
		rs = ps.executeQuery();
		while(rs.next())
		{
			id.add(rs.getString("id"));
		}
		
		DbConnection.free(conn, ps, rs);
		return id;
	}
	
	public static String getShiYongbm(String dw_mc) throws SQLException, ClassNotFoundException{
		String shiyongbm=null;
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		String sql = "select dw_id from think_danwei where dw_mc='"+dw_mc+"'";
		
		conn = DbConnection.getConnection();
		ps = conn.prepareStatement(sql);
		rs = ps.executeQuery();
		while(rs.next())
		{
			shiyongbm = rs.getString("dw_id");
			////System.out.println(sq_id);
		}
		DbConnection.free(conn, ps, rs);
		return shiyongbm;
	}
	
	public static String getShenhe(String name) throws SQLException, ClassNotFoundException{
		String in_name = "";
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		String sql = "select distinct(sq_id) from think_shenhe where sh_ren = '"+name+"'";
		conn = DbConnection.getConnection();
		ps = conn.prepareStatement(sql);
		rs = ps.executeQuery();
		
		while(rs.next())
		{
			in_name+=rs.getString("sq_id")+",";
		}
		
		DbConnection.free(conn, ps, rs);
		if(in_name.length()==0)
		{
			return "0";
		}
		else
		{
			return in_name.substring(0,in_name.length()-1);
		}
		
	}
	
	public static void main(String args[])throws Exception
	{
		System.out.println(getMaxBH("J"));
	}

}
